[PATCH] Correctly handle signed 32-bit time_t types
authorNoah Meyerhans <noahm@debian.org>
Wed, 4 Feb 2026 16:09:21 +0000 (11:09 -0500)
committerNoah Meyerhans <noahm@debian.org>
Wed, 4 Feb 2026 16:09:21 +0000 (11:09 -0500)
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1124541
Forwarded: no

dovecot handles 32-bit time_t in a couple of different ways, but neither quite
works currently.  Setting TIME_T_MAX_BITS to 31 isn't correctly handled in
places where time_t values are constructed, for example in
io_loop_get_wait_time().

Similarly, setting TIME_T_MAX_BITS = 32 and defining TIME_T_SIGNED is not
correctly handled by tm_is_too_large().

This change fixes tm_is_too_large() to set max_time to the correct maximum date
representable by a signed 32-bit time_t.

Closes: #1124541
Gbp-Pq: Name Correctly_handle_signed_32-bit_time_t_types.patch

src/lib-imap/imap-date.c
src/lib-imap/test-imap-date.c

index c9262932c1b011051e51b87dfe8fc40447bbdbae..df9d11605725391d9e60c3887caa4c92ec8293d6 100644 (file)
@@ -86,7 +86,11 @@ static bool tm_is_too_large(const struct tm *tm, time_t *max_time_r)
 
        if (max_time == 0) {
 #if TIME_T_MAX_BITS == 32
+#ifdef TIME_T_SIGNED
+               max_time = ((time_t)1 << TIME_T_MAX_BITS - 1) - 1;
+#else
                max_time = 0xffffffffUL;
+#endif
 #elif TIME_T_MAX_BITS == 64
                max_time = 0xffffffffffffffffULL;
 #else
index 5a2a36402ff61fe5539e780a1aca022c6fca098b..94c1a820dae111eb5d80c511098399739dc1d6a5 100644 (file)
@@ -15,7 +15,7 @@ static void test_imap_date(void)
        } tests[] = {
                { "01-Jan-1970", 0 },
                { "19-Jan-2038", 2147472000 },
-#if TIME_T_MAX_BITS >= 32
+#if TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED)
                { "07-Feb-2106", 4294944000 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -25,10 +25,10 @@ static void test_imap_date(void)
                { "31-Dec-9999", 253402214400LL },
 #endif
                /* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS == 32 && !defined(TIME_T_SIGNED)
                { "20-Jan-2038", 2147483647 },
                { "31-Dec-9999", 2147483647 },
-#elif TIME_T_MAX_BITS == 32
+#elif TIME_T_MAX_BITS == 32 && !defined(TIME_T_SIGNED)
                { "08-Feb-2106", 4294967295 },
                { "31-Dec-9999", 4294967295 },
 #endif
@@ -59,8 +59,10 @@ static void test_imap_datetime(void)
        } tests[] = {
                { "01-Jan-1970 00:00:00 +0000", 0, 0 },
                { "19-Jan-2038 03:14:07 +0000", 2147483647, 0 },
-               { "19-Jan-2038 05:14:07 +0200", 2147483647, 2*60 },
 #if TIME_T_MAX_BITS >= 32
+               { "19-Jan-2038 05:14:07 +0200", 2147483647, 2*60 },
+#endif
+#if TIME_T_MAX_BITS > 32 || !defined(TIME_T_SIGNED)
                { "07-Feb-2106 06:28:15 +0000", 4294967295, 0 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -71,10 +73,10 @@ static void test_imap_datetime(void)
                { "31-Dec-9999 23:59:59 -2359", 253402300799LL + 23*60*60 + 59*60, -23*60 - 59 },
 #endif
                /* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS == 32 && defined(TIME_T_SIGNED)
                { "19-Jan-2038 03:14:08 +0000", 2147483647, 0 },
                { "31-Dec-9999 23:59:59 -2359", 2147483647, -23*60 - 59 },
-#elif TIME_T_MAX_BITS == 32
+#elif TIME_T_MAX_BITS == 32 && !defined(TIME_T_SIGNED)
                { "07-Feb-2106 06:28:16 +0000", 4294967295, 0 },
                { "31-Dec-9999 23:59:59 -2359", 4294967295, -23*60 - 59 },
 #endif